package com.xunlai.infra.permission.manager.authority.login;

import com.xunlai.infra.permission.common.exception.ExceptionCodeEnum;
import com.xunlai.infra.permission.manager.authority.exception.BadCaptchaException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import java.util.regex.Pattern;

/**
 * 双因子认证 失败处理类
 * @author yanguoliang
 * @date 2020/9/1 16:19
 */
@Slf4j
@RequiredArgsConstructor
public class TwoFactorAuthenticationFailureHandler extends AbstractResponseBodyAuthenticationFailureHandler {

    @Override
    protected AuthenticationException handleFailureInternal(HttpServletRequest request, AuthenticationException exception) {
        log.error(exception.getMessage(),exception);
        return exception;
    }

    @Override
    protected int failureStatus(HttpServletRequest request, AuthenticationException exception) {
        if(exception instanceof BadCaptchaException){
            return ExceptionCodeEnum.ERROR_CAPTCHA.getCode();
        }else {
            return super.failureStatus(request, exception);
        }
    }

    @Override
    protected String failureMsg(HttpServletRequest request, AuthenticationException exception) {
        if(exception instanceof BadCaptchaException){
            return ExceptionCodeEnum.ERROR_CAPTCHA.getDesc();
        }else {
            return super.failureMsg(request, exception);
        }
    }
}
